/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sohu.sip.auth.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sohu.peak.util.Logger;
import com.sohu.sip.auth.business.bean.UserItemBean;
import com.sohu.sip.auth.business.event.UserItemAddEvent;
import com.sohu.sip.auth.business.event.UserItemDeleteEvent;
import com.sohu.sip.auth.business.event.UserItemListEvent;
import com.sohu.sip.auth.business.result.UserItemAddResult;
import com.sohu.sip.auth.business.result.UserItemDeleteResult;
import com.sohu.sip.auth.business.result.UserItemListResult;
import com.sohu.sip.auth.constant.AuthConstant;
import com.sohu.sip.auth.util.ParamUtil;

/**
 * @author luciali
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UserItemAction extends BaseAction {
	private static Logger log = new Logger();
	private String userId;

	public String addUserItem() {
		return "addUserItem";
	}

	public String listUserItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			UserItemListEvent event = new UserItemListEvent(ParamUtil.getLong(request, "userId").longValue(),
					ParamUtil.getLong(request, "itemId").longValue(),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			UserItemListResult result = UserItemBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listUserItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String listUserItem2() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			UserItemListEvent event = new UserItemListEvent(ParamUtil.getLong(request, "userId").longValue(),
					ParamUtil.getLong(request, "itemId").longValue(),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			UserItemListResult result = UserItemBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listUserItem2";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String listLeftUserItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			UserItemListEvent event = new UserItemListEvent(ParamUtil.getLong(request, "userId").longValue(),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			UserItemListResult result = UserItemBean.getInstance().listLeft(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listLeftUserItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String insertUserItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			UserItemAddEvent event = new UserItemAddEvent(ParamUtil.getLong(request, "userId").longValue(),
					(String[])request.getParameterValues("mid"));
			UserItemAddResult result = UserItemBean.getInstance().add(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				response.sendRedirect("useritem_listLeftUserItem.sip?userId=" + ParamUtil.getLong(request, "userId"));
				return null;
			} else {
				return "failure";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}
	
	public String deleteItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			UserItemDeleteEvent event = new UserItemDeleteEvent((String[])request.getParameterValues("mid"));
			UserItemDeleteResult result = UserItemBean.getInstance().delete(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				return "success";
				//return null;
			} else {
				return "failure";
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return "failure";
		}
	}

	public String success() {
		return "success";
	}

	public String failure() {
		return "failure";
	}

}
